#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

using namespace std;

QTcpSocket *client;

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    client = new QTcpSocket(this);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_connect_clicked()
{
    //获取ip和端口号与服务器连接
    QString ip = ui->lineEdit_ip->text();
    qint16  port = ui->lineEdit_port->text().toShort();
    client->connectToHost(ui->lineEdit_ip->text(),port);
}

void Widget::on_pushButton_log_in_clicked()
{
    root["cmd"] = "timekeeperLogin_req";
    root["userid"] = ui->lineEdit_userid->text().toStdString();
    root["password"] = ui->lineEdit_password->text().toStdString();
    //将密码通过json格式传给服务器
    qDebug() << root.toStyledString().c_str();
    client->write(writer.write(root).c_str());
    //等待服务器回应密码是否正确，正确则进入功能界面
    client->waitForReadyRead(3000);//阻塞等待read
    client->read(jsonstr, sizeof(jsonstr));
    reader.parse(jsonstr,root);
    qDebug() << root.toStyledString().c_str();
    if(strcmp(root["cmd"].asString().c_str(), "timekeeperLogin_OK") == 0){
        sel = new class select();
        this->hide();
        sel->show();
    }else{
        QMessageBox::warning(this, "密码错误", root["cmd"].asString().c_str(), QMessageBox::Yes, QMessageBox::Yes);
    }
}
